package blue.mqtt4j.client;

import blue.mqtt4j.client.codec.Decoder;
import blue.mqtt4j.client.codec.JsonDecoder;

/**
 * @author Jin Zheng
 * @since 2020-10-07
 */
public class MqttClientMain
{
	public MqttClientMain()
	{
	}

	public static void main(String[] args) throws Exception
	{
		Decoder decoder = new JsonDecoder();
		LoggerConsumerListener consumerListener = new LoggerConsumerListener();
		LoggerConnectionListener connectionListener = new LoggerConnectionListener();
		MqttOptions options = MqttOptions.newMqttOptions();
		MqttClient mqttClient = MqttClient.newClient("localhost:1883", options);
		mqttClient.setConnectionListener(connectionListener);
		mqttClient.connect();
		mqttClient.subscribe(MqttTopic.from("test/+", MqttQoS.EXACTLY_ONCE), decoder, consumerListener);
		Producer producer = mqttClient.getProducer();
		for (int i = 0; i < 3; i++)
		{
			MqttQoS qos = MqttQoS.valueOf(i % 3);
			producer.sendAsync(MqttMessage.from("test/t_" + i, qos, "async_" + i + "_" + qos));
			//producer.sendSync(MqttMessage.from("test/t_" + i, qos, "sync_" + i + "_" + qos));
		}
		System.out.println("Finished");
		Thread.sleep(2000);
		mqttClient.unsubscribe("test/+");

		Thread.sleep(20000);
		mqttClient.close();
	}
}
